<!-- 用户角色 管理 -->
<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- 搜索栏-->
      <div v-if="crud.props.searchToggle">
        <el-row :gutter="10">
          <el-col :xs="24" :sm="8" :md="7" :lg="6">
            <dep-tree :dep-input-change="depSearchInput" :load-on-mounted="true" />
          </el-col>
          <el-col :xs="24" :sm="16" :md="17" :lg="18">
            <el-input v-model="query.name" clearable size="small" placeholder="可输入角色名称搜索" style="max-width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />

            <rrOperation />
          </el-col>
        </el-row>

      </div>
      <!-- 按钮-->
      <crud-opts :permission="permission" />
    </div>
    <!-- 表格-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      border
      style="width: 100% "
      row-key="id"
      stripe
      tooltip-effect="dark"
      :highlight-current-row="true"
      @sort-change="crud.sortChange"
      @selection-change="crud.selectionChangeHandler"
      @current-change="handleCurrentChange"
    >
      <el-table-column type="selection" width="39" />
      <!--<el-table-column type="index" width="45" />-->

      <el-table-column prop="name" label="角色名称" sortable="custom" />

      <el-table-column prop="code" :show-overflow-tooltip="true" label="角色代码" sortable="custom" />

      <el-table-column prop="depName" :show-overflow-tooltip="true" label="所属组织" />

      <el-table-column
        prop="status"
        label="状态"
        width="80"
        sortable="custom"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            class="switch"
            :width="50"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="启用"
            inactive-text="禁用"
            :disabled="!permission.has('edit')"
            @change="crud.changeStatusWithNum(scope.row, scope.row.status, {'title':'角色','column':'name'})"
          />
        </template>
      </el-table-column>
      <el-table-column prop="paixu" label="排序" width="80" header-align="center" align="center" sortable="custom" />

      <!-- 角色类型 0：普通角色 ；16：系统内置角色不可删除 -->
      <!-- 智能推断为字典类型, 字典类型为 dict.sys_role_type -->
      <el-table-column prop="type" label="角色类型" sortable="custom" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <!-- 智能推断为字典类型, 字典类型为 dict.sys_role_type  -->
          {{ dict.label.sys_role_type[scope.row.type] || '未知字典码:' + scope.row.type }}
        </template>
      </el-table-column>

      <!-- 使用范围 0：所有组织可用， 1：当前组织以及下级组织可用，2：仅限当前组织可用 -->
      <!-- 智能推断为字典类型, 字典类型为 dict.sys_role_use_range -->
      <el-table-column prop="useRange" label="使用范围" sortable="custom" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <!-- 智能推断为字典类型, 字典类型为 dict.sys_role_use_range  -->
          {{ dict.label.sys_role_use_range[scope.row.useRange] || '未知字典码:' + scope.row.useRange }}
        </template>
      </el-table-column>

      <el-table-column prop="createTime" label="创建时间" :show-overflow-tooltip="true" header-align="center" sortable="custom" />

      <el-table-column prop="remark" label="备注" sortable="custom" :show-overflow-tooltip="true" />
      <el-table-column fixed="right" label="操作" width="120" header-align="center" align="center">
        <template slot-scope="scope">
          <!-- 操作按钮，简介操作的（不需要详情和扩展的），可以只引用 udOperation -->
          <udOperation
            :data="scope.row"
            :permission="permission"
          >
            <template v-if="permission.has('SysRoleUser:query') || permission.has('SysRoleUser:query2')" slot="afterEdit">
              <el-button
                size="mini"
                type="success"
                style="width: 80px"
                @click="accredit(scope.row)"
              >
                授权信息
              </el-button>
            </template>
          </udOperation>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页插件, 如果不需要注释掉下面一行,以及引入的 pagination 组件即可 -->
    <pagination />
    <!-- 表单 -->
    <SysRoleForm :show-binding="showBinding" />
    <!-- 详情, 如果用不到，注释下面一行 -->
    <SysRoleInfo />

    <bind-user :show-binding="showBinding" :bind-role-id="selectRoleId" :bind-role-name="selectRoleName" />
  </div>
</template>
<!-- 用户角色 首页 -->
<script>
import crudApi, { page } from '@/api/sys/Role'
import SysRoleForm from './form'
import SysRoleInfo from './info'
import CRUD, { presenter, header } from '@crud/crud'
import CrudOpts from '@crud/CRUD.operation'
import rrOperation from '@crud/RR.operation'
// import udOperation from '@crud/UD.operation'
import udOperation from '@crud/RUD.operation'
import pagination from '@crud/Pagination'
import DepTree from '@/views/components/depTree'
// 授权绑定用户
import bindUser from './RoleUser/bindUser'
export default {
  dicts: [
    'sys_role_type',
    'sys_role_use_range',
    'common_status'
  ], // 字典
  components: {
    CrudOpts,
    rrOperation,
    udOperation,
    pagination,
    SysRoleInfo,
    SysRoleForm,
    DepTree,
    bindUser
  },
  cruds() {
    return CRUD({ title: '用户角色', initDataMethod: page, crudMethod: { ...crudApi }})
  },
  mixins: [presenter(), header()],
  data() {
    return {
      permission: new Map(),
      showBinding: false,
      selectRoleId: '',
      selectRoleName: ''
    }
  },
  mounted() {
    this.permission = this.$store.state.menu.buttonPermission.get('SysRole')
  },
  methods: {
    [CRUD.HOOK.beforeRefresh]() {
      this.$emit('selectedChanged', '', '')
      return true
    },
    // 选中一个菜单
    handleCurrentChange(row) {
      this.$emit('selectedChanged', row.id, row.name, row.code)
    },
    // 授权
    accredit(row) {
      this.selectRoleId = row.id
      this.selectRoleName = row.name
      this.showBinding = true
      this.crud.toAdd()
    },
    depSearchInput(node) {
      console.log(node)
      this.query.depId = node
    }

  }
}
</script>

<style scoped>

</style>
